package com.uenpay.im.panel.more;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.uenpay.im.R;

import java.util.ArrayList;

/**
 * @author SL
 * @date 2014/7/12
 * @copyRight http://www.qiyunxin.com
 */
public class MoreGridAdapter extends BaseAdapter {
	private ArrayList<MoreEntity> arrayList;
	private LayoutInflater _inflater;
	private IMoreOnClickListener mIMoreOnClickListener;

	public MoreGridAdapter(Context context, ArrayList<MoreEntity> _arrayList,
			IMoreOnClickListener _IMoreOnClickListener) {
		arrayList = _arrayList;
		_inflater = LayoutInflater.from(context);
		mIMoreOnClickListener = _IMoreOnClickListener;
	}

	@Override
	public int getCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public MoreEntity getItem(int i) {
		return arrayList == null ? null : arrayList.get(i);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(final int i, View view, ViewGroup viewGroup) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = _inflater.inflate(R.layout.item_more_layout, viewGroup,false);
			holder.type_btn = (Button) view.findViewById(R.id.type_btn);
			holder.type_name = (TextView) view.findViewById(R.id.type_name);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		holder.type_btn.setBackgroundResource(arrayList.get(i).imageResourceId);
		holder.type_name.setText(arrayList.get(i).textResourceId);
		holder.type_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mIMoreOnClickListener.onMoreClick(arrayList.get(i).moreType);
			}
		});
		return view;
	}

	class ViewHolder {
		private Button type_btn;
		private TextView type_name;
	}
}
